package edu.wit.acl.controller;


import edu.wit.acl.entity.Permission;
import edu.wit.common.annotation.Log;
import edu.wit.common.enums.BusinessType;
import edu.wit.common.result.R;
import edu.wit.common.utils.ServletUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 权限 菜单管理
 * </p>
 *
 * @author testjava
 * @since 2020-01-12
 */
@RestController
@RequestMapping("/admin/acl/permission")
//@CrossOrigin
@Api(tags = "菜单模块")
public class PermissionController extends BaseController{

    //获取全部菜单
    @ApiOperation(value = "查询所有菜单")
    @GetMapping
    @Log(title = "查询所有菜单", businessType = BusinessType.SELECT)
    public R indexAllPermission() {
        String username = ServletUtils.getRequest().getHeader("username");
        System.out.println("=======================================================查询所有菜单");
        List<Permission> list = permissionService.queryAllMenu(userService.selectByUsername(username).getCategory());
//        List<Permission> list = permissionService.queryAllMenu(2);

        return R.ok().data("children", list);
    }

    @ApiOperation(value = "递归删除菜单")
    @DeleteMapping("remove/{id}")
    @Log(title = "递归删除菜单", businessType = BusinessType.DELETE)
    public R remove(@PathVariable String id) {
        permissionService.removeChildById(id);
        return R.ok();
    }

    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/doAssign")
    @Log(title = "给角色分配权限", businessType = BusinessType.INSERT)
    public R doAssign(String roleId, String[] permissionId) {
        permissionService.saveRolePermissionRealtionShip(roleId, permissionId);
        return R.ok();
    }

    @ApiOperation(value = "根据角色获取菜单")
    @GetMapping("toAssign/{roleId}")
    @Log(title = "根据角色获取菜单", businessType = BusinessType.SELECT)
    public R toAssign(@PathVariable String roleId) {
        List<Permission> list = permissionService.selectAllMenu(roleId);
        return R.ok().data("children", list);
    }

    @ApiOperation(value = "根据角色获取菜单(用户端分配权限)")
    @GetMapping("selectMenuByRoleId/{roleId}")
    @Log(title = "根据角色获取菜单(用户端分配权限)", businessType = BusinessType.SELECT)
    public R selectMenuByRoleId(@PathVariable String roleId) {
        List<Permission> list = permissionService.selectMenuByRoleId(roleId);
        return R.ok().data("children", list);
    }

    @ApiOperation(value = "新增菜单")
    @PostMapping("save")
    @Log(title = "新增菜单", businessType = BusinessType.INSERT)
    public R save(@RequestBody Permission permission) {
        permissionService.save(permission);
        return R.ok();
    }

    @ApiOperation(value = "修改菜单")
    @PutMapping("update")
    @Log(title = "修改菜单", businessType = BusinessType.UPDATE)
    public R updateById(@RequestBody Permission permission) {
        permissionService.updateById(permission);
        return R.ok();
    }

}

